﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using WAU.DTOs.AppPermissionModule;
using WAU.Models.AppUserModels;
using WAU.Models.Framework;
using WAU.Services;

namespace WAU.Controllers
{
    /// <summary>
    /// 角色、菜单、权限 Controller
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class AppPermissionController : EssentialController
    {
        private readonly AppPermissionService _appPrmssnService;

        /// <summary>
        /// ctor
        /// </summary>
        /// <param name="appPrmssnService"></param>
        public AppPermissionController(AppPermissionService appPrmssnService)
        {
            _appPrmssnService = appPrmssnService;
        }

        #region 菜单

        /// <summary>
        /// 获取菜单列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetAppMenuList(ListAppMenuDto dto)
        {
            List<AppMenuListItemSCO> dataList = await _appPrmssnService.GetAppMenuList(dto);
            return Ok(new Result
            {
                Success = true,
                Data = dataList,
                Total = dataList.Count
            });
        }

        /// <summary>
        /// 获取菜单列表（供下拉选择时使用）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetAppMenuListForSelect()
        {
            List<AppMenu> dataList = await _appPrmssnService.GetAppMenuListForSelect();
            return Ok(new Result
            {
                Success = true,
                Data = dataList,
                Total = dataList.Count
            });
        }

        /// <summary>
        /// 获取菜单详情
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetAppMenuDetail(DetailAppMenuDto dto)
        {
            ExecResult<AppMenu> result = await _appPrmssnService.GetAppMenuDetail(dto.Id);
            return Ok(new Result
            {
                Success = result.Success,
                Data = result.Data,
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 编辑菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> EditAppMenu(EditAppMenuDto dto)
        {
            ExecResult result = await _appPrmssnService.EditAppMenu(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 删除菜单（及其子项）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> DeleteAppMenu(DeleteAppMenuDto dto)
        {
            ExecResult result = await _appPrmssnService.DeleteAppMenu(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }

        #endregion

        #region 角色

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetAppRoleList(ListAppRoleDto dto)
        {
            var dataList = await _appPrmssnService.GetAppRoleList(dto);
            return Ok(new Result
            {
                Success = true,
                Data = dataList,
                Total = dataList.Count
            });
        }

        /// <summary>
        /// 获取角色详情
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetAppRoleDetail(DetailAppRoleDto dto)
        {
            ExecResult<AppRole> result = await _appPrmssnService.GetAppRoleDetail(dto);
            return Ok(new Result
            {
                Success = result.Success,
                Data = result.Data,
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> EditAppRole(EditAppRoleDto dto)
        {
            ExecResult result = await _appPrmssnService.EditAppRole(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> DeleteAppRole(DeleteAppRoleDto dto)
        {
            ExecResult result = await _appPrmssnService.DeleteAppRole(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = result.Success,
                Msg = result.ErrorMsg
            });
        }

        #endregion

        #region 菜单 和 角色

        /// <summary>
        /// 获取某一角色的菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetRoleMenus(GetRoleMenusDto dto)
        {
            var dataList = await _appPrmssnService.GetRoleMenus(dto);   // 表示角色id
            return Ok(new Result
            {
                Success = true,
                Data = dataList
            });
        }

        /// <summary>
        /// 设置某一角色的菜单
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> SetRoleMenus(SetRoleMenusDto dto)
        {
            ExecResult result = await _appPrmssnService.SetRoleMenus(CurrentUserId, dto);
            return Ok(new Result
            {
                Success = true,
                Msg = result.ErrorMsg
            });
        }

        #endregion

        #region 权限

        /// <summary>
        /// 获取用户权限
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> FetchUserPermission()
        {
            List<AppMenuListItemSCO> menuDatas = await _appPrmssnService.FetchUserPermission(CurrentUserRoleCodes);
            return Ok(new Result
            {
                Success = true,
                Data = menuDatas
            });
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetUserRoles(GetUserRolesDto dto)
        {
            List<AppRole> menuDatas = await _appPrmssnService.GetUserRoles(dto);
            return Ok(new Result
            {
                Success = true,
                Data = menuDatas
            });
        }


        /// <summary>
        /// 设置用户角色（未将用户的登录失效）
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> SetUserRoles(SetUserRolesDto dto)
        {
            ExecResult result = await _appPrmssnService.SetUserRoles(CurrentUserId, dto);
            return Ok(new Result 
            { 
                Success = result.Success, 
                Msg = result.ErrorMsg
            });
        }

        /// <summary>
        /// 获取给定角色的用户列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ActionResult> GetRoleUserList(GetRoleUsersDto dto)
        {
            var result = await _appPrmssnService.GetRoleUserList(dto.RoleId, dto);
            return Ok(new Result
            {
                Success = true,
                Data = result.DataList,
                Total = result.Total
            });
        }

        #endregion
    }
}
